package com.hcb.tonguetiphealth.adapter;

import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.hcb.tonguetiphealth.global.Global;
import com.hcb.tonguetiphealth.manager.AsyncBitmapLoader;
import com.hcb.tonguetiphealth.manager.AsyncBitmapLoader.ImageCallBack;
import com.hcb.tonguetiphealth.model.ResDish;
import com.hcb.tonguetiphealth.model.Restaurant;
import com.hcb.tonguetiphealth.util.StringUtils;
import com.hcb.tonguetiphealth.view.R;
import com.hcb.tonguetiphealth.widget.bookOnline.DishOrderWidget;

public class DishOrderListAdapter extends BaseAdapter {

	private Context mContext;
	private List<ResDish> mList;
	private LayoutInflater vi;
	private AsyncBitmapLoader asyncBitmapLoader;
	private Builder mDialog;
	private DishOrderWidget dishOrderWidget;
	
	public DishOrderListAdapter(DishOrderWidget dishOrderWidget, Context context,List<ResDish> list) {
		mContext = context;
		mList = list;
		asyncBitmapLoader = AsyncBitmapLoader.getInstance();
		mDialog = new AlertDialog.Builder(mContext);
		this.dishOrderWidget = dishOrderWidget;
	}
	
	public int getCount() {
		if(mList == null) {
			return 0;
		}
		return mList.size();
	}
	
	public Object getItem(int position) {
		if(mList == null) {
			return null;
		} else {
			return mList.get(position);
		}
	}
	
	public long getItemId(int position) {
		return position;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {

		View view;
		
		if(convertView == null) {
			vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = (LinearLayout) vi.inflate(R.layout.dish_order_list_item, null);
		} else {
			view = convertView;
		}
		FillDataIntoView(view, position);
		
		return view;
	}
	
	private void FillDataIntoView(View view, final int position) {
		final ResDish dish = mList.get(position);

		ImageView img = (ImageView)view.findViewById(R.id.dish_order_dish_img);
		TextView tv_name = (TextView)view.findViewById(R.id.dish_order_name_textview);
		TextView tv_books = (TextView)view.findViewById(R.id.dish_order_num_textview);
		TextView tv_price = (TextView)view.findViewById(R.id.dish_order_price_textview);
		TextView tv_totalprice = (TextView)view.findViewById(R.id.dish_order_total_price_textview);
		Button btCancel = (Button)view.findViewById(R.id.dish_order_cancel_button);
		
		tv_name.setText(dish.getTitle());
		tv_books.setText(dish.getOrderNum());
		tv_price.setText(dish.getPrice());
		float price = StringUtils.parseFloat(dish.getPrice());
		float num = StringUtils.parseFloat(dish.getOrderNum());
		float totalPrice = price * num;
		tv_totalprice.setText(totalPrice + "");
		btCancel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showRemoveDialog(dish.getTitle(),position);
			}
		});
		
		String imageURL = dish.getDishThumb(); 
        Bitmap bitmap = asyncBitmapLoader.loadBitmap(img, imageURL, new ImageCallBack() {  
              
            public void imageLoad(ImageView imageView, Bitmap bitmap) {  
                // TODO Auto-generated method stub  
                imageView.setImageBitmap(bitmap);  
            }  
        });  
		if(bitmap == null) {    
			img.setImageResource(R.drawable.ic_launcher);    
		} else {    
		    img.setImageBitmap(bitmap);    
		}
	    
	}
	
	protected void showRemoveDialog(String message, final int position){
        mDialog.setTitle("我的菜单")
        .setMessage("从菜单中移除 \"" + message + "\" ?");
        mDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				Global.resDishOrderList.remove(position);
				dishOrderWidget.setAdapter();
			}
		});
        mDialog.setNegativeButton("取消", null);
        
        mDialog.setCancelable(true);
        mDialog.show();
    }
}
